package nio.util;

import io.netty.util.internal.StringUtil;

import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.Selector;

public class BaseUtil {

    public static Boolean flag = false;

    public static void logBufferInfo(Buffer buffer,String string){
        if (!flag){
            return;
        }

        int limit = buffer.limit();
        int position = buffer.position();
        int capacity = buffer.capacity();
        if (!StringUtil.isNullOrEmpty(string)){
            System.out.println("------------------"+string+"------------------");
        }
        System.out.println("["+"limit:"+limit+",position:"+position+",capacity:"+capacity+"]");
    }

    public static void printByteBuffer(ByteBuffer buffer){
        while (buffer.hasRemaining()){
            byte b = buffer.get();
            System.out.println((char) b);
            logBufferInfo(buffer,"");
        }

    }

    public static void logSwitch(Boolean flag){
        BaseUtil.flag = flag;
    }
}
